package com.financing.platform.web.api;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.financing.platform.common.Pager;
import com.financing.platform.common.ResultCode;
import com.financing.platform.common.ResultMap;
import com.financing.platform.common.ResultMap.STATUS;
import com.financing.platform.dto.MessageJpushDTO;
import com.financing.platform.dto.PhonePushDTO;
import com.financing.platform.service.IJPushMessageService;
import com.financing.platform.service.IJPushService;
import com.financing.platform.util.DTOdateSet;
import com.financing.platform.util.StringUtil;

/**
 * 极光推送
 * @author qiye
 *
 */
@Controller("JPushAction")
@RequestMapping("/api/jPush")
@Scope("prototype")
public class JPushAction extends BaseAction {
	
	private Logger log = Logger.getLogger(this.getClass());
	
	@Autowired
	private IJPushService jPushService;
	
	@Autowired
	private IJPushMessageService jPushMessageService;
	/***
	 * app推送 消息推送到所有手机
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping("/jPush.shtml")
	@ResponseBody
	public ResultMap jPush(HttpServletRequest request,
			HttpServletResponse response,PhonePushDTO dto) throws Exception{
		try {
		     Map<String,String> extraMap = new HashMap<String,String>();
		     extraMap.put("redirct",request.getParameter("redirct"));
		     dto.setExtraMap(extraMap);
		     List<String> alisesList = new ArrayList<String>();
		     dto.setAliseList(alisesList);
			jPushService.pushAllPhone(dto);
			resultMap.setStatus(STATUS.SUCCESS);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
		} catch (Exception e) {
			log.error("error ycfproductInfojob",e);
			resultMap.setStatus(STATUS.SUCCESS);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
		}
		log.info("jPush end.....................");
		return resultMap;
	}
	
	@RequestMapping("/addjPushMessage.shtml")
	@ResponseBody
	public ResultMap addjPushMessage(HttpServletRequest request,
			HttpServletResponse response,MessageJpushDTO dto) throws Exception{
		try {
			log.info("add jPush message start.....................");
			if(StringUtil.isNullOrEmptyStr(dto.getFpJpushMsgType()) || StringUtil.isNullOrEmptyStr(dto.getFpJpushContent()) || StringUtil.isNullOrEmptyStr(dto.getFpJpushRedrict())){
				resultMap.setStatus(STATUS.ERROR);
				resultMap.setStatusCode(ResultCode.STATUS_CODE_PARAMETERS_LOSE);
				return resultMap;
			}
			DTOdateSet.setCreateInfo(dto, request.getSession());
			DTOdateSet.setUpdateInfo(dto, request.getSession());
			jPushMessageService.addJpushMessage(dto);
			resultMap.setStatus(STATUS.SUCCESS);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
		} catch (Exception e) {
			log.error("error addjPushMessage",e);
			resultMap.setStatus(STATUS.SUCCESS);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
		}
		log.info("add jPush message end.....................");
		return resultMap;
	}
	
	@RequestMapping("/updatejPushMessage.shtml")
	@ResponseBody
	public ResultMap updatejPushMessage(HttpServletRequest request,
			HttpServletResponse response,MessageJpushDTO dto) throws Exception{
		try {
			log.info("update jPush message start.....................");
			if(StringUtil.isNullOrEmptyStr(dto.getFpJpushMsgType()) || StringUtil.isNullOrEmptyStr(dto.getFpJpushContent()) || StringUtil.isNullOrEmptyStr(dto.getFpJpushRedrict())){
				resultMap.setStatus(STATUS.ERROR);
				resultMap.setStatusCode(ResultCode.STATUS_CODE_PARAMETERS_LOSE);
				return resultMap;
			}
			
			/**
			 * 定时发送 设置时间
			 */
//			if("02".equals(dto.getFpJpushSendType())){
//				DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
//				dto.setFpJpushSendTime(df.parse(dto.getFpJpushSendTimeStr()));
//			}else{
//				dto.setFpJpushSendTime(null);
//			}
			
			DTOdateSet.setUpdateInfo(dto, request.getSession());
			jPushMessageService.updateJpushMessage(dto);
			resultMap.setStatus(STATUS.SUCCESS);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
		} catch (Exception e) {
			log.error("error addjPushMessage",e);
			resultMap.setStatus(STATUS.SUCCESS);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
		}
		log.info("update jPush message end.....................");
		return resultMap;
	}
	
	@RequestMapping("/deljPushMessage.shtml")
	@ResponseBody
	public ResultMap deljPushMessage(HttpServletRequest request,
			HttpServletResponse response,MessageJpushDTO dto) throws Exception{
		try {
			log.info("update jPush message start.....................");
			if(dto.getId() == null){
				resultMap.setStatus(STATUS.ERROR);
				resultMap.setStatusCode(ResultCode.STATUS_CODE_PARAMETERS_LOSE);
				return resultMap;
			}
			
			List<MessageJpushDTO> list = jPushMessageService.queryJpushMessageList(dto);
			
			if(null != list && list.size() >= 1 && "01".equals(list.get(0).getFpJpushSendStatus())){
				dto.setDeleteTag("2");
				DTOdateSet.setUpdateInfo(dto, request.getSession());
				jPushMessageService.updateJpushMessage(dto);
			}
			
			resultMap.setStatus(STATUS.SUCCESS);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
		} catch (Exception e) {
			log.error("error addjPushMessage",e);
			resultMap.setStatus(STATUS.SUCCESS);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
		}
		log.info("update jPush message end.....................");
		return resultMap;
	}
	
	@RequestMapping("/listjPushMessage.shtml")
	@ResponseBody
	public ResultMap listjPushMessage(HttpServletRequest request,
			HttpServletResponse response,MessageJpushDTO dto,Pager pager) throws Exception{
		try {
			log.info("list jPush message start.....................");
			dto.setFpJpushMsgType("02");
			dto.setPageNo(pager.getPageNo());
			dto.setPageSize(pager.getPageSize());
			int totalCount = jPushMessageService.queryMessageJpushCount(dto);
			pager.setTotalCount(totalCount);
			resultMap.setAttribute("pager", pager);
			resultMap.setAttribute("list",jPushMessageService.queryJpushMessageList(dto));
			resultMap.setStatus(STATUS.SUCCESS);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
		} catch (Exception e) {
			log.error("error listjPushMessage",e);
			resultMap.setStatus(STATUS.SUCCESS);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
		}
		log.info("list jPush message end.....................");
		return resultMap;
	}
	
	
}
